import java.io.*;
import java.util.*;
import java.util.zip.*;
import datolib.*;

public class UipTool {


	public static boolean createArchive(DatoliteApplication application) {
  		
  		
  		try { 
  		
  			File inFolder=new File(application.getDesignDirectory());
  			
   			File outFolder=new File(application.getApplicationID() + ".uip");
 			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFolder)));
 			BufferedInputStream in = null;
 
 			byte[] data  = new byte[1000];
 			String files[] = inFolder.list();
 
 			for (int i=0; i<files.length; i++) {
  				
  				in = new BufferedInputStream(new FileInputStream (inFolder.getPath() + "/" + files[i]), 1000);
  	
				out.putNextEntry(new ZipEntry(files[i])); 
  
  				int count;
  
  				while((count = in.read(data,0,1000)) != -1) {
 					out.write(data, 0, count);
  				}
  				out.closeEntry();
  			}
  			
  			out.flush();
  			out.close();
  			
  			application.setUipFile(application.getApplicationID() + ".uip");
  			
  			return true;
  			
  		} catch(Exception e) {
  			e.printStackTrace();
  			return false;
  		} 
	}
	
	public static void sendUIP(DatoliteInstance instance, TcpGateway socket) {
		
    	BufferedInputStream inStream = null;
    		
		try {
    		
    		inStream = new BufferedInputStream(new FileInputStream(instance.getUipFile()));	
    		
    		final byte[] buffer = new byte[4029];
    
    		for (int read = inStream.read(buffer); read >= 0; read = inStream.read(buffer)) {

        		socket.write(buffer, 0, read);
        	}
    	
    		inStream.close();
			
			
			
    	} catch (IOException ioe) {
    		// oh noes
    	}
		
	}

}
